package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.Answer;
import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IQuestionService;
import cn.wolfcode.trip.base.service.impl.QuestionServiceImpl;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/questions")
public class QuestionController {
    @Autowired
    private IQuestionService questionService;

    /**
     * 分页查询问题
     * @param qo
     * @return
     */
    @GetMapping
    public PageInfo<Question> list(QueryObject qo) {
        qo.setOrderBy("q.createTime desc");
        PageInfo<Question> pageInfo = questionService.query(qo);
        return pageInfo;
    }

    /**
     * 保存发布问题
     */
    @PostMapping
    public JsonResult save(Question question) {
        questionService.save(question);
        return new JsonResult();
    }

    /**
     * 根据id查询对用的问题内容
     * @param qo
     * @return
     */
    @GetMapping("{questionId}")
    public Question getQuestionById(@PathVariable Long questionId) {
        return questionService.selectById(questionId);
    }

    /**
     * 根据问题id查询对应的回答
     * @param
     * @return
     */
    @GetMapping("{questionId}/answers")
    public List<Answer> getAnswers(@PathVariable Long questionId) {
        return questionService.selectAnswersById(questionId);
    }
}
